<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>选择会员卡</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/app/admin/component/layui/css/layui.css">
</head>
<body>
    <div style="padding: 20px;">
        <form class="layui-form" action="">
            <div class="layui-form-item">
                <label class="layui-form-label">会员卡号</label>
                <div class="layui-input-block">
                    <input type="text" name="card_no" required lay-verify="required" placeholder="请输入会员卡号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="formSubmit">确定</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
        
        <table class="layui-hide" id="cardTable" lay-filter="cardTable"></table>
    </div>

    <script src="/app/admin/component/layui/layui.js"></script>
    <script>
    layui.use(['form', 'table'], function(){
        var form = layui.form;
        var table = layui.table;
        var $ = layui.$;
        
        // 渲染表格
        table.render({
            elem: '#cardTable'
            ,url: '/app/ky_shouyin/admin/member/getUnusedCards'
            ,cols: [[
                {field:'card_no', title:'卡号', width:180}
                ,{field:'created_at', title:'生成时间', width:180}
                ,{fixed: 'right', title:'操作', toolbar: '#tableBar', width:120}
            ]]
            ,page: true
        });
        
        // 监听表格工具条
        table.on('tool(cardTable)', function(obj){
            var data = obj.data;
            if(obj.event === 'select'){
                // 返回卡号给父窗口
                var index = parent.layer.getFrameIndex(window.name);
                parent.layui.$("#card_no").val(data.card_no);
                parent.layer.close(index);
            }
        });
        
        // 监听表单提交
        form.on('submit(formSubmit)', function(data){
            $.ajax({
                url: '/app/ky_shouyin/admin/member/selectCard'
                ,type: 'post'
                ,data: data.field
                ,success: function(res){
                    if(res.code === 0){
                        // 返回卡号给父窗口
                        var index = parent.layer.getFrameIndex(window.name);
                        parent.layui.$("#card_no").val(res.data.card_no);
                        parent.layer.close(index);
                    } else {
                        layer.msg(res.msg);
                    }
                }
            });
            return false;
        });
    });
    </script>
    
    <script type="text/html" id="tableBar">
        <a class="layui-btn layui-btn-xs" lay-event="select">选择</a>
    </script>
</body>
</html> 